package server.controller.tenant;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import plug.result.RetResponse;
import plug.result.RetResult;
import server.config.exception.BizException;
import server.dao.MoneyRecordDao;
import server.dao.TenantDao;
import server.model.entity.Tenant;
import server.model.vo.TenantVo;
import server.service.TenantService;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.channels.Pipe;

/**
 * @description:
 * @author: 许哲
 * @createTime: 2021/11/21 17:45
 * @version: 1.0
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("v2/api/tenant")
public class tenantV2Controller {
    @Autowired
    private TenantService tenantService;

    @Autowired
    private MoneyRecordDao moneyRecordDao;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TenantDao tenantDao;

    @PostMapping("/pay")
    @ApiOperation("获取支付链接")
    public RetResult getPayUrl(@RequestBody TenantVo tenantVo) throws UnsupportedEncodingException {
        return tenantService.createTenant(tenantVo);
    }

    @GetMapping("/pay/{indentOrder}/info")
    @ApiOperation("获取支付结果")
    public RetResult getStatus(@PathVariable String indentOrder) {
        Tenant tenant = tenantDao.findById(request.getHeader("token")).get();
        if(tenant == null){
            throw new BizException("error token");
        }
        return RetResponse.makeOKRsp(moneyRecordDao.findByIndentOrder(indentOrder).getStatus());
    }
}
